﻿namespace REVISIONMAX20
{
    using System;
    using System.ComponentModel;
    using System.Data.SQLite;
    using System.Diagnostics;
    using System.Drawing;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;

    public class MyLogList : UserControl
    {
        private ObjectBase _parent;
        private UIHandler _uiHandler = null;
        private ToolStripButton btnClear;
        private ToolStripButton btnDumpLogs;
        private ColumnHeader columnHeader1;
        private ColumnHeader columnHeader2;
        private IContainer components = null;
        private MyListView lvLogs;
        private ToolStrip toolStrip1;
        private ToolStripSeparator toolStripSeparator1;
        private ToolStripSeparator toolStripSeparator2;

        public MyLogList()
        {
            this.InitializeComponent();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            using (SQLiteConnection connection = Common.GetSQLiteConnection())
            {
                connection.Open();
                using (SQLiteCommand command = connection.CreateCommand())
                {
                    command.CommandText = string.Format("DELETE FROM `logs` where parentid = {0}", this._parent.id);
                    command.ExecuteNonQuery();
                }
            }
            this.UpdateViewer();
        }

        private void btnDumpLogs_Click(object sender, EventArgs e)
        {
            StringBuilder builder = new StringBuilder();
            foreach (REVISIONMAX20.Log log in Common.GetLogList(this._parent))
            {
                builder.AppendLine(log.value);
            }
            StreamWriter writer = new StreamWriter(@".\log.txt", true);
            writer.Write(builder.ToString());
            writer.Close();
            Process.Start(@".\log.txt");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.btnClear = new System.Windows.Forms.ToolStripButton();
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.btnDumpLogs = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            this.lvLogs = new REVISIONMAX20.MyListView(this.components);
            this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
            this.toolStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
            // 
            // btnClear
            // 
            this.btnClear.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnClear.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnClear.Name = "btnClear";
            this.btnClear.Size = new System.Drawing.Size(36, 22);
            this.btnClear.Text = "Clear";
            this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
            // 
            // toolStrip1
            // 
            this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.btnClear,
            this.toolStripSeparator1,
            this.btnDumpLogs,
            this.toolStripSeparator2});
            this.toolStrip1.Location = new System.Drawing.Point(0, 0);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Size = new System.Drawing.Size(544, 25);
            this.toolStrip1.TabIndex = 17;
            this.toolStrip1.Text = "toolStrip1";
            // 
            // btnDumpLogs
            // 
            this.btnDumpLogs.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnDumpLogs.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnDumpLogs.Name = "btnDumpLogs";
            this.btnDumpLogs.Size = new System.Drawing.Size(63, 22);
            this.btnDumpLogs.Text = "Dump Logs";
            this.btnDumpLogs.Click += new System.EventHandler(this.btnDumpLogs_Click);
            // 
            // toolStripSeparator2
            // 
            this.toolStripSeparator2.Name = "toolStripSeparator2";
            this.toolStripSeparator2.Size = new System.Drawing.Size(6, 25);
            // 
            // lvLogs
            // 
            this.lvLogs.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lvLogs.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader1,
            this.columnHeader2});
            this.lvLogs.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lvLogs.FullRowSelect = true;
            this.lvLogs.GridLines = true;
            this.lvLogs.HideSelection = false;
            this.lvLogs.Location = new System.Drawing.Point(0, 25);
            this.lvLogs.Name = "lvLogs";
            this.lvLogs.Size = new System.Drawing.Size(544, 222);
            this.lvLogs.TabIndex = 18;
            this.lvLogs.UseCompatibleStateImageBehavior = false;
            this.lvLogs.View = System.Windows.Forms.View.Details;
            this.lvLogs.DoubleClick += new System.EventHandler(this.lvLogs_DoubleClick);
            // 
            // columnHeader1
            // 
            this.columnHeader1.Text = "Info";
            this.columnHeader1.Width = 348;
            // 
            // columnHeader2
            // 
            this.columnHeader2.Text = "created";
            this.columnHeader2.Width = 119;
            // 
            // MyLogList
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.lvLogs);
            this.Controls.Add(this.toolStrip1);
            this.Name = "MyLogList";
            this.Size = new System.Drawing.Size(544, 247);
            this.toolStrip1.ResumeLayout(false);
            this.toolStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        public void Initzialize(UIHandler puiHandler, ObjectBase parent)
        {
            this._uiHandler = puiHandler;
            this._parent = parent;
            this.UpdateViewer();
        }

        private void lvLogs_DoubleClick(object sender, EventArgs e)
        {
            foreach (ListViewItem item in this.lvLogs.SelectedItems)
            {
                REVISIONMAX20.Log logById = Common.GetLogById((int) item.Tag, this._parent);
                this._uiHandler.ViewLog(logById);
                break;
            }
        }

        public void UpdateViewer()
        {
            this.lvLogs.Items.Clear();
            BindingSource source = new BindingSource();
            using (SQLiteConnection connection = Common.GetSQLiteConnection())
            {
                connection.Open();
                using (SQLiteCommand command = connection.CreateCommand())
                {
                    command.CommandText = string.Format("select * from `logs` where parentid = {0} order by `created`", this._parent.id);
                    SQLiteDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        string text = reader.GetValue(4).StringValueOrEmpty();
                        if (text.Length > 0x19)
                        {
                            text = text.Substring(0, 0x19);
                        }
                        ListViewItem item = this.lvLogs.Items.Add(text);
                        item.Tag = reader.GetInt32(0);
                        item.SubItems.Add(reader.GetValue(2).StringValueOrEmpty());
                    }
                }
            }
        }

        public UIHandler uiHandler
        {
            get
            {
                return this._uiHandler;
            }
            set
            {
                this._uiHandler = value;
            }
        }
    }
}

